#include "LeagueSelectionMenu.h"
#include "LeagueStatsMenu.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "LeagueManager.h"
#include "League.h"
#include "UserManager.h"
#include "User.h"

LeagueSelectionMenu::LeagueSelectionMenu(MenuManager* const i_menuManager)
	:Menu(i_menuManager)
{
}

LeagueSelectionMenu::~LeagueSelectionMenu(void)
{
}

void LeagueSelectionMenu::Draw() const
{
	STD::cout << Print::Title("LEAGUE SELECTION MENU:");
	STD::cout << STD::endl;

	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);

	size_t counter = 0;

	STD::cout << "Available leagues:" << STD::endl;
	for (LeagueVecCit it = leagues.begin(); it != leagues.end(); ++it)
	{
		const League& league = *(*it);
		STD::cout << Utilities::TabUpTo( Utilities::IntToStlString(++counter) + ")", 4);
		STD::cout << Utilities::TabUpTo(league.GetName(), 25);
		
		const UsersList& users = GetSP()->GetUserMgr().GetUserList();
		for (size_t i = 0; i<users.size(); ++i)
			if(users.at(i)->m_team->GetLeagueID() == league.GetID())
				STD::cout << "(" << i+1 << ") ";

		STD::cout << STD::endl;
	}
	STD::cout << STD::endl;

	STL::string options = "1-" + Utilities::IntToStlString(counter) + ") Select league.\n0) Return to previous menu'.";
	STD::cout	<< Print::Options(options)
				<< "Selection: ";
}

bool LeagueSelectionMenu::Input(const STL::string& i_input)
{
	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);
	bool goodInput = true;

	size_t res = Utilities::StringToInt(i_input);

	if (res == 0)
		GetMM()->SetMenu(GetMM()->GetMainMenu());
	else if (res > 0 && res <= leagues.size())
	{
		LeagueStatsMenu* nextMenu = static_cast<LeagueStatsMenu*>(GetMM()->GetLeagueStatsMenu());
		nextMenu->SetLeagueID(res-1);
		GetMM()->SetMenu(nextMenu);
	}
	else
		goodInput = false;

	return goodInput;
}

